//
//  WWCategoryViewController.m
//  YiQiTingShu
//
//  Created by WeiWei on 15/11/4.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "WWCategoryViewController.h"
#import "AFTools.h"
#import "WWTableCollectionCell.h"
#import "PublicHeader.h"
#import "MenuControlView.h"
#import "WWTableContentCollectionCell.h"
#import "AlbumFViewController.h"
#import <MBProgressHUD.h>

@interface WWCategoryViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, MenuControlViewDelegate, WWTableCollectionCellDelegate, WWTableContentCollectionCellDelegate>

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) MenuControlView *menuView;
@property (nonatomic, strong) NSMutableArray *headerArr;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger lastMenu;
@property (nonatomic, strong) MBProgressHUD *progressView;

@end

@implementation WWCategoryViewController

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.itemSize = CGSizeMake(GViewWidth(self.view), GViewHeight(self.view) - 64 - FitConstant_YorHeight(50));
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, FitConstant_YorHeight(50), self.view.frame.size.width, GViewHeight(self.view) - 64 - FitConstant_YorHeight(50)) collectionViewLayout:flowLayout];
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        //注册cell  ww
        [self.collectionView registerClass:[WWTableCollectionCell class] forCellWithReuseIdentifier:@"wwCell"];
        [self.collectionView registerClass:[WWTableContentCollectionCell class] forCellWithReuseIdentifier:@"contentCell"];
        //注册cell  menuCell
       [self.collectionView registerClass:[WWTableCollectionCell class] forCellWithReuseIdentifier:@"menuCell"];
        
    }
    return _collectionView;
}

#pragma mark 视图将出现时,上下隐藏
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = NO;
//    self.tabBarController.tabBar.hidden = YES;
}

- (void)getData {
    [AFTools getDataByURLString:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=%ld&contentType=album&device=iPhone&scale=2&version=4.3.20", _categoryId] WithDataBlock:^(id data) {
        NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"tags"]];
        NSArray *array = [NSArray arrayWithArray:[dictionary objectForKey:@"list"]];
        for (NSDictionary *dic in array) {
            [self.dataArray addObject:[dic objectForKey:@"tname"]];
        }
        NSMutableArray *tempArray = [NSMutableArray arrayWithObject:@"推荐"];
        [tempArray addObjectsFromArray:self.dataArray];
        self.menuView.dataSource = [NSArray arrayWithArray:tempArray];
        NSDictionary *dict = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"focusImages"]];
        NSArray *tempArr = [NSArray arrayWithArray:[dict objectForKey:@"list"]];
        for (NSDictionary *dic in tempArr) {
            [self.headerArr addObject:[dic objectForKey:@"pic"]];
        }
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"categoryContents"]];
        NSMutableArray *tmpArray = [NSMutableArray arrayWithArray:[dic objectForKey:@"list"]];
        [(WWTableCollectionCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] handleData:self.headerArr withArray:tmpArray];
        [self.collectionView reloadData];
        [_progressView removeFromSuperview];
    }];
}

#pragma mark 初始化menuView
- (MenuControlView *)menuView
{
    if (!_menuView) {
        _menuView = [[MenuControlView alloc] initWithFrame:CGRectMake(0, 0, FitConstant_XorWidth(375), FitConstant_YorHeight(50))];
        _menuView.delegate = self;
        _menuView.numberOfMenu = 4;
    }
    return _menuView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = _categoryTitle;
    self.dataArray = [NSMutableArray array];
    self.headerArr = [NSMutableArray array];
    [self getData];
    self.menuView.delegate = self;
    _progressView = [[MBProgressHUD alloc] initWithView:self.collectionView];
    _progressView.labelText = @"努力加载中,请稍等";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.58 green:0.6 blue:0.61 alpha:0.3];
    [self.view addSubview:_progressView];
    [self.view addSubview:self.menuView];
    [self.view addSubview:self.collectionView];
    [self.collectionView NightWithType:UIViewColorTypeNormal];
    [self.menuView NightWithType:UIViewColorTypeNormal];
    [self.view NightWithType:UIViewColorTypeNormal];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return (self.dataArray.count + 1);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        WWTableCollectionCell *wwTableCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"wwCell" forIndexPath:indexPath];
        wwTableCell.delegate = self;
        return wwTableCell;
    } else {
        WWTableContentCollectionCell *wwConCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"contentCell" forIndexPath:indexPath];
        wwConCell.conDelegate = self;
        return wwConCell;
    }
    
}

#pragma mark - 代理
- (void)passValue:(NSString *)trackID withAlumbTitle:(NSString *)alumbTitel withAlbumId:(NSString *)albumId {
    AlbumFViewController *vc = [[AlbumFViewController alloc] init];
    vc.albumId = albumId;
    vc.trackID = trackID;
    vc.alumbTitel = alumbTitel;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)pass:(NSString *)trackID withAlumbTitle:(NSString *)alumbTitel withAlbumId:(NSString *)albumId {
    AlbumFViewController *vc = [[AlbumFViewController alloc] init];
    vc.albumId = albumId;
    vc.trackID = trackID;
    vc.alumbTitel = alumbTitel;
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //切换页面
    self.page = (NSInteger)(scrollView.contentOffset.x + FitConstant_XorWidth(50)) / scrollView.frame.size.width;
    if (_lastMenu > self.page) {
        _lastMenu = self.page;
        self.menuView.collectionView.contentOffset = CGPointMake(FitConstant_XorWidth(92.5) * self.page, 0);
    }
    else{
        _lastMenu = self.page;
        self.menuView.collectionView.contentOffset = CGPointMake(FitConstant_XorWidth(92.5) * self.page, 0);
        if (_lastMenu < self.menuView.dataSource.count - 4) {
            self.menuView.collectionView.contentOffset = CGPointMake(FitConstant_XorWidth(92.5) * self.page, 0);
        }
        else{
            _lastMenu = self.page;
            if (_lastMenu <= self.menuView.dataSource.count - 4) {
                self.menuView.collectionView.contentOffset = CGPointMake(FitConstant_XorWidth(92.5) * self.page, 0);
            }
        }
    }
    
    
    [_menuView selectCell:self.page];
    if (self.collectionView.contentOffset.x) {
        NSInteger i = self.collectionView.contentOffset.x / self.collectionView.frame.size.width;
        [AFTools getDataByURLString:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=%ld&device=iPhone&pageId=1&pageSize=20&status=0&tagName=%@", _categoryId, self.dataArray[i - 1]] WithDataBlock:^(id data) {
            [(WWTableContentCollectionCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] handleData:data];
            [self.collectionView reloadData];
        }];
    }
}

#pragma mark - MenuViewDelegate
-(void)didSelectedMenu:(NSInteger)number
{
    self.collectionView.contentOffset = CGPointMake(self.collectionView.frame.size.width * number, 0);
    if (number) {
        [AFTools getDataByURLString:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=%ld&device=iPhone&pageId=1&pageSize=20&status=0&tagName=%@", _categoryId, self.dataArray[number - 1]] WithDataBlock:^(id data) {
            [(WWTableContentCollectionCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:number inSection:0]] handleData:data];
            [self.collectionView reloadData];
        }];
    }
    
}

@end
